#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/**
 * @brief 通过手机号查找用户
 * @param data 系统数据
 * @param userPhone 用户手机号
 * @return 用户指针，未找到返回NULL
 */
User* findUser(SystemData* data, const char* userPhone) {
    if (data == NULL || userPhone == NULL) {
        return NULL;
    }
    
    User* currentUser = data->userList;
    while (currentUser != NULL) {
        if (strcmp(currentUser->phone, userPhone) == 0) {
            return currentUser;
        }
        currentUser = currentUser->next;
    }
    
    return NULL;
}

/**
 * @brief 获取当前系统时间
 * @param buffer 存储返回值的容器
 * @param bufferSize 容器的容量，不小于30
 */
void getTime(char* buffer, size_t bufferSize) {
    if (buffer == NULL || bufferSize < 30) { // 检查参数有效性
        return;
    }
    
    time_t t = time(NULL);
    struct tm* tm_info = localtime(&t);
    strftime(buffer, bufferSize, "%Y-%m-%d %H:%M:%S", tm_info);
}

/**
/**
 * @brief 获取字符串格式的通知类别
 * @param type 通知类别(NotificationType 类型)
 * @return 返回结果字符串
 */
const char* getNotificationTypeString(NotificationType type) {
    switch (type) {
    case NOTIFICATION_ARRIVAL:
        return "包裹到达通知";
    case NOTIFICATION_PICKUP:
        return "包裹取走通知";
    case NOTIFICATION_REMINDER:
        return "取件提醒通知";
    case NOTIFICATION_DAMAGED:
        return "包裹损坏通知";
    case NOTIFICATION_LOST:
        return "包裹丢失通知";
    case NOTIFICATION_RETURNED:
        return "包裹退回通知";
    case NOTIFICATION_WRONG_PICKUP:
        return "错误取件通知";
    case NOTIFICATION_SYSTEM:
        return "系统通知";
    default:
        return "未知通知类型";
    }
}

/**
 * @brief 记录操作日志
 * @param data 系统数据结构指针
 * @param username 用户名
 * @param operation 操作描述
 * @return 成功返回OK，失败返回错误码
 */
ErrorCode logOperation(SystemData* data, const char* username, const char* operation) {
    // 检查参数
    if (data == NULL || operation == NULL) {
        return ERR_INVALID_PARAM;
    }
    
    // 检查用户权限
    if (username == NULL) {
        return ERR_NO_PERMISSION;
    }
    
    // 系统操作或有权限的用户可以记录日志
    if (strcmp(username, "SYSTEM") != 0) {
        User* user = findUser(data, username);
        if (user == NULL || !hasPermission(user, PERM_LOG_VIEW)) {
            return ERR_NO_PERMISSION;
        }
    }
    
    // 分配内存
    Log* log = (Log*)malloc(sizeof(Log));
    if (log == NULL) {
        return ERR_MEMORY;
    }
    
    // 填充日志信息
    strncpy(log->username, username, MAX_USERNAME_LEN - 1);
    log->username[MAX_USERNAME_LEN - 1] = '\0'; // 确保字符串以null结尾
    
    strncpy(log->operation, operation, MAX_DESCRIPTION_LEN - 1);
    log->operation[MAX_DESCRIPTION_LEN - 1] = '\0'; // 确保字符串以null结尾
    
    log->operationTime = time(NULL);
    log->next = NULL;
    
    // 添加到日志链表
    if (data->logList == NULL) {
        data->logList = log;
    } else {
        Log* current = data->logList;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = log;
    }
    
    return OK;
}

char* my_strptime(const char* str, const char* format, struct tm* tm) {
    int year, month, day, hour = 0, min = 0, sec = 0;

    if (strcmp(format, "%Y-%m-%d") == 0) {
        if (sscanf(str, "%d-%d-%d", &year, &month, &day) != 3) {
            return NULL;
        }
        tm->tm_year = year - 1900;
        tm->tm_mon = month - 1;
        tm->tm_mday = day;
        return (char*)(str + 10); // 返回未解析部分的指针
    }

    if (strcmp(format, "%Y-%m-%d %H:%M:%S") == 0) {
        if (sscanf(str, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec) != 6) {
            return NULL;
        }
        tm->tm_year = year - 1900;
        tm->tm_mon = month - 1;
        tm->tm_mday = day;
        tm->tm_hour = hour;
        tm->tm_min = min;
        tm->tm_sec = sec;
        return (char*)(str + 19); // 返回未解析部分的指针
    }

    return NULL; // 不支持的格式
}
